﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sausage.Web.Code
{
    public class ImageResult : ActionResult
    {
        public string SourceFilename { get; set; }
        public MemoryStream SourceStream { get; set; }
        public string ContentType { get; set; }

        public ImageResult(string sourceFilename, string contentType)
        {
            SourceFilename = sourceFilename;
            ContentType = contentType;
        }

        public ImageResult(MemoryStream sourceStream, string contentType)
        {
            SourceStream = sourceStream;
            ContentType = contentType;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            response.Clear();
            response.Cache.SetCacheability(HttpCacheability.Public);

            response.ContentType = ContentType;

            if (SourceStream != null)
            {
                SourceStream.WriteTo(response.OutputStream);

            }
            else
            {
                response.TransmitFile(SourceFilename);
            }
        }
    }
}